{% extends "panel.html" %}
{% load i18n %}
{% load inventree_extras %}
{% load plugin_extras %}

{% block label %}plugin{% endblock %}


{% block heading %}
{% trans "Plugin Settings" %}
{% endblock %}

{% block content %}

<div class='alert alert-block alert-danger'>
    {% trans "Changing the settings below require you to immediately restart the server. Do not change this while under active usage." %}
</div>

<div class='table-responsive'>
<table class='table table-striped table-condensed'>
    <tbody>
        {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_SCHEDULE" icon="fa-calendar-alt" %}
        {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_EVENTS" icon="fa-reply-all" %}
        {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_URL" icon="fa-link" %}
        {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_NAVIGATION" icon="fa-sitemap" %}
        {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_APP" icon="fa-rocket" %}
        {% include "InvenTree/settings/setting.html" with key="PLUGIN_ON_STARTUP" %}
        <tr><td colspan='5'></td></tr>
        {% include "InvenTree/settings/setting.html" with key="PLUGIN_CHECK_SIGNATURES" %}
    </tbody>
</table>
</div>

{% plugins_enabled as plug %}

<div class='panel-heading'>
    <div class='d-flex flex-wrap'>
        <h4>{% trans "Plugins" %}</h4>
        {% include "spacer.html" %}
        <div class='btn-group' role='group'>
            {% url 'admin:plugin_pluginconfig_changelist' as url %}
            {% include "admin_button.html" with url=url %}
            {% if plug %}
            <button class="btn btn-success" id="install-plugin" title="{% trans 'Install Plugin' %}"><span class='fas fa-plus-circle'></span> {% trans "Install Plugin" %}</button>
            {% endif %}
        </div>
    </div>
</div>

{% if not plug %}
<div class='alert alert-warning alert-block'>
    {% trans "External plugins are not enabled for this InvenTree installation" %}<br>
</div>
{% endif %}

<div class='table-responsive'>
<table class='table table-striped table-condensed'>
    <thead>
        <tr>
            <th>{% trans "Name" %}</th>
            <th>{% trans "Key" %}</th>
            <th>{% trans "Author" %}</th>
            <th>{% trans "Date" %}</th>
            <th>{% trans "Version" %}</th>
            <th></th>
        </tr>
    </thead>

    <tbody>
        {% plugin_list as pl_list %}
        {% if pl_list %}
        <tr><td colspan="6"><h6>{% trans 'Active plugins' %}</h6></td></tr>
        {% for plugin_key, plugin in pl_list.items %}
            {% include "InvenTree/settings/plugin_details.html" with plugin=plugin plugin_key=plugin_key %}
        {% endfor %}
        {% endif %}

        {% inactive_plugin_list as in_pl_list %}
        {% if in_pl_list %}
        <tr><td colspan="6"><h6>{% trans 'Inactive plugins' %}</h6></td></tr>
        {% for plugin_key, plugin in in_pl_list.items %}
            {% include "InvenTree/settings/plugin_details.html" with plugin=plugin plugin_key=plugin_key %}
        {% endfor %}
        {% endif %}
    </tbody>
</table>
</div>


{% plugin_errors as pl_errors %}
{% if pl_errors %}
<div class='panel-heading'>
    <div class='d-flex flex-wrap'>
        <h4>{% trans "Plugin Error Stack" %}</h4>
        {% include "spacer.html" %}
    </div>
</div>

<div class='table-responsive'>
    <table class='table table-striped table-condensed'>
        <thead>
            <tr>
                <th>{% trans "Stage" %}</th>
                <th>{% trans "Name" %}</th>
                <th>{% trans "Message" %}</th>
            </tr>
        </thead>

        <tbody>
        {% for stage, errors in pl_errors.items %}
            {% for error_detail in errors %}
            {% for name, message in error_detail.items %}
            <tr>
                <td>{{ stage }}</td>
                <td>{{ name }}</td>
                <td>{{ message }}</td>
            </tr>
            {% endfor %}
            {% endfor %}
        {% endfor %}
        </tbody>
    </table>
    </div>
{% endif %}

{% endblock %}
